﻿namespace CachePage
{
    using System;
    using System.Web.UI;
    using System.Web.UI.Adapters;

    public class PageStateAdapter : PageAdapter
    {
        private string defaultGuidKey = "";

        private bool CachePageAdapterCheckpoint()
        {
            return ((base.Page.Request.Form["__VIEWSTATEGUID"] != null) || CacheUtility<bool>.GetByContext(base.Page.Request.AppRelativeCurrentExecutionFilePath, ExtensionsConfigSection.ConfigPath, new CacheUtility<bool>.NullCase(this.GetNullCachePageAdapterCheckpoint)));
        }

        private bool CachePageAdapterUpdateTriggers()
        {
            return CacheUtility<bool>.GetByContext(base.Page.Request.AppRelativeCurrentExecutionFilePath + " :updateTriggers", ExtensionsConfigSection.ConfigPath, new CacheUtility<bool>.NullCase(this.GetNullCachePageAdapterUpdateTriggers));
        }

        private bool GetNullCachePageAdapterCheckpoint(string key)
        {
            string str = key;
            if (!ExtensionsConfigSection.HasExtensionsConfigSection())
            {
                return false;
            }
            bool flag = false;
            foreach (string str2 in ExtensionsConfigSection.GetCachePageAdapterAddPath())
            {
                if (str.StartsWith("~" + str2))
                {
                    flag = true;
                    break;
                }
            }
            if (flag)
            {
                foreach (string str2 in ExtensionsConfigSection.GetCachePageAdapterRemovePath())
                {
                    if (str.StartsWith("~" + str2))
                    {
                        return false;
                    }
                }
            }
            return flag;
        }

        private bool GetNullCachePageAdapterUpdateTriggers(string key)
        {
            string str = key;
            if (ExtensionsConfigSection.HasExtensionsConfigSection())
            {
                foreach (string str2 in ExtensionsConfigSection.GetCachePageAdapterUpdateTriggersPath())
                {
                    if (str.StartsWith("~" + str2))
                    {
                        return true;
                    }
                }
                return false;
            }
            return false;
        }

        public override PageStatePersister GetStatePersister()
        {
            if (this.CachePageAdapterCheckpoint())
            {
                return new GuidCachePageStatePersister(base.Page, this.defaultGuidKey, ExtensionsConfigSection.GetCacheTimeOut());
            }
            return base.GetStatePersister();
        }

        public void SetDefaultGuidKey(string pageGuidKey)
        {
            this.defaultGuidKey = pageGuidKey;
        }

        public bool IsExtensionsPage
        {
            get
            {
                return this.CachePageAdapterCheckpoint();
            }
        }

        public bool IsUpdateTriggers
        {
            get
            {
                return this.CachePageAdapterUpdateTriggers();
            }
        }
    }
}

